package com.shilin.watchsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shilin.watchsystem.entity.Admin;
import com.shilin.watchsystem.form.AddAdminForm;
import com.shilin.watchsystem.mapper.AdminMapper;
import com.shilin.watchsystem.service.IAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shilin.watchsystem.service.exception.ExceptionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.Action;
import java.beans.beancontext.BeanContext;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2020-08-21
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {

    @Resource
    AdminMapper adminMapper;
    @Autowired
    ExceptionService exceptionService;

    @Override
    public void insert(AddAdminForm form) {
        Admin admin = new Admin();
        BeanUtils.copyProperties(form,admin);
        save(admin);
    }

    @Override
    public IPage<Admin> page(Page<Admin> page) {
        return adminMapper.page(page);
    }

    @Override
    public void login(String account, String password, HttpServletRequest request) {
        QueryWrapper<Admin> wr = new QueryWrapper<>();
        wr.eq("del",0);
        wr.eq("account",account);
        wr.eq("password",password);
        List<Admin> list = list(wr);
        if(list.size()<=0){
            exceptionService.throwException(444,"账号或密码错误");
        }
        request.getSession().setAttribute("admin",account);
    }
}
